package com.newtouch.avmarketing.mgr.modules.avmarkrting.service.impl;

import com.newtouch.avmarketing.mgr.modules.avmarkrting.entity.dto.DoubleAuditingDto;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.DoubleAuditingDtoService;
import com.newtouch.avmarketing.mgr.modules.avmarkrting.service.VideoPlayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

@Service
public class VideoPlayServiceImpl implements VideoPlayService {
    private FileInputStream fileIn;
    private ByteBuffer byteBuf;
    private long fileLength;
    private int arraySize;
    private byte[] array;

    @Autowired
    private DoubleAuditingDtoService doubleAuditingDtoService;
    @Override
    public byte[] getVideoPlay(HttpServletResponse response, String policyCode) throws Exception{
//        DoubleAuditingDto doubleAuditingDto = doubleAuditingDtoService.findDoubleAuditingDto(policyCode);
//        if (doubleAuditingDto != null) {
//            String path = doubleAuditingDto.getVideoPath();
//            System.out.println("path:" + path);
//
//            byte data[] = readInputStream(path);
//            System.out.println("data:"+data);
//            response.setContentType("video/mp4"); //设置返回的文件类型
//            OutputStream os = response.getOutputStream();
//            os.write(data);
//            System.out.println("data:"+data);
//            os.flush();
//            os.close();
//
//            return data;
//        }
        return null;
    }

    public void readInputStream(HttpServletResponse response,String policyCode) throws Exception{

        DoubleAuditingDto doubleAuditingDto = doubleAuditingDtoService.findDoubleAuditingDto(policyCode);
        if (doubleAuditingDto != null) {
            String path = doubleAuditingDto.getVideoPath();
            System.out.println("path:" + path);

            FileInputStream fileInputStream = new FileInputStream(path);
            FileChannel fileChannel = fileInputStream.getChannel();
            ByteBuffer byteBuf = ByteBuffer.allocate(65536);
            int bytes = fileChannel.read(byteBuf);// 读取到ByteBuffer中

            byte[] byteArray = new byte[byteBuf.remaining()];

            response.setContentType("video/mp4"); //设置返回的文件类型
            ServletOutputStream sos = response.getOutputStream();
            long start = System.currentTimeMillis();
            while(bytes != -1){
                array = new byte[bytes];// 字节数组长度为已读取长度
                byteBuf.flip();
                byteBuf.get(byteArray);// 从ByteBuffer中得到字节数组
                byteBuf.clear();
            }
            long end = System.currentTimeMillis();
            System.out.println("ChannelFileReader: " + (end - start));
            sos.flush();
            sos.close();
        }
    }

    @Override
    public String getFilePath(String policyCode) throws Exception {
        DoubleAuditingDto doubleAuditingDto = doubleAuditingDtoService.findDoubleAuditingDto(policyCode);
        if (doubleAuditingDto != null) {
            return doubleAuditingDto.getVideoPath();
        }
        return "";
    }
}
